package com.wcity.manage.web.property;

import com.wcity.manage.common.bean.RetBean;
import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.ComplaintManage;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.dao.model.Voice;
import com.wcity.manage.service.UserService;
import com.wcity.manage.service.property.PropertyComplaintService;
import com.wcity.manage.service.property.PropertyVoiceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 前端百姓心声
 * Created by Administrator on 2016/12/19.
 */
@Controller
@RequestMapping("complaint")
public class IPropertyComplaintAct {

    @Resource private UserService userService;
    @Resource private PropertyComplaintService propertycomplaintService;

    /**
     * 根据小区id获取投诉列表
     * @param pageModel
     * @param blockId
     * @return
     */
    @RequestMapping ("i_complaint_list")
    @ResponseBody
    public Map<String,Object> IComplaintList(PageModel pageModel, String blockId) {

        if(!StringUtil.isBlank( blockId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        ComplaintManage complaintManage = new ComplaintManage();
        complaintManage.setBlockId( Integer.parseInt(blockId) );
        pageModel.setParams(complaintManage);
        Map<String,Object> map = propertycomplaintService.getIComplaintManageList( pageModel );
        return map;
    }

    /**
     * 发布投诉
     * @param content
     * @param images
     * @param blockId
     * @param regId
     * @return
     */
    @RequestMapping ("complaint_add")
    @ResponseBody
    public RetBean complaintAdd (String content , String images , String blockId , String regId ) {
        if( !StringUtil.isBlank( content ) )
            new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        String errorMsg = propertycomplaintService.complaintManageAdd( content , images , blockId , regId );
        if( StringUtil.isBlank( errorMsg )) throw new CustomException( errorMsg, Code.ERROR );
        return new RetBean( Code.SUCCESS, errorMsg );
    }

    /**
     * 获取我的声音
     * @param regId
     * @return
     */
    @RequestMapping ("i_complaint")
    @ResponseBody
    public Map<String,Object> IVoices (PageModel pageModel,String regId) {

        if( StringUtil.isBlank( regId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        Voice voice = new Voice();
        voice.setRegId( Integer.parseInt( regId ) );
        pageModel.setParams( voice );
        Map<String,Object> map = propertycomplaintService.getIComplaintManageList( pageModel );

        return map;
    }

    /**
     * 获取投诉详情
     * @param id
     * @return
     */
    @RequestMapping ("i_complaint_info")
    @ResponseBody
    public Map<String,Object> IVoiceInfo (String id , HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        if( StringUtil.isBlank( id )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        try{
            ComplaintManage complaintManage = propertycomplaintService.getIComplaintManageInfo ( id );
            User user = userService.getUser( complaintManage.getUserId() );
            map.put("worker",user.getUserName());
            map.put("workerTel",user.getMobile());
            map.put("id",complaintManage.getId());
            map.put("accountNum",complaintManage.getAccountNum());
            map.put("content",complaintManage.getContent());
            map.put("status",complaintManage.getStatus());
            map.put("image",complaintManage.getImage());
            map.put("remark",complaintManage.getRemark());
            map.put("createTime",complaintManage.getCreateTime());
            map.put("userId",complaintManage.getUserId());
            map.put("phone",complaintManage.getPhone());
            map.put("reply",complaintManage.getReply());
            map.put("replyTime",complaintManage.getReplyTime());
            map.put("blockId",complaintManage.getBlockId());
            map.put("regId",complaintManage.getRegId());
            map.put("retCode",Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }
}
